package com.itheima.service.impl;



import com.itheima.common.JwtToken;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishFlavorsMapper;
import com.itheima.mapper.DishMapper;


import com.itheima.pojo.dto.dishDto.DishAddDto;
import com.itheima.pojo.dto.dishDto.DishFlavors;
import com.itheima.pojo.dto.dishDto.DishUpdateDto;

import com.itheima.pojo.vo.categoryVo.GetCategoryVo;
import com.itheima.pojo.vo.dishVo.*;
import com.itheima.service.DishService;
import com.itheima.common.AuthThreadLocal;


import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * @author tcotk
 */
@Service
public class DishServiceImpl implements DishService {

    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorsMapper dishFlavorsMapper;
  @Resource
  private CategoryMapper categoryMapper;

    @Override
    public List<DishByPageVo> selectByPage(int page, int pageSize, String name) {
        List<DishByPageVo> dishList = new ArrayList<>();
        page = (page - 1) * pageSize;
        for (DishByPageVo dish : dishMapper.selectByPage(page, pageSize, name)) {
            List<GetDishFlavors> list = dishFlavorsMapper.selectById(dish.getId());
            dish.setFlavors(list);

            dishList.add(dish);
        }

        return dishList;
    }

    @Override
    public List<GetDishVo> selectAll() {
        List<GetDishVo> dishVoList = new ArrayList<>();
        for (GetDishVo dish : dishMapper.selectAll()) {
            List<GetDishFlavors> list = dishFlavorsMapper.selectById(dish.getId());
            dish.setFlavors(list);
            dishVoList.add(dish);
        }

        return dishVoList;

    }

    @Override
    public GetDishByIdVo selectById(String id) {
        GetDishByIdVo dish = dishMapper.selectById(id);
        dish.setFlavors(dishFlavorsMapper.selectById(id));
        return dish;

    }

    @Override
    public boolean add(DishAddDto dish) {
        dish.setCreateTime(LocalDateTime.now());
        String username = JwtToken.parse(AuthThreadLocal.getAuth());
        dish.setCreateUser(username);
        dish.setCode("1");
        List<DishFlavors> flavors = dish.getFlavors();
        int result = 0;
        for (DishFlavors flavor : flavors) {
            flavor.setCreateUser(username);
            flavor.setCreateTime(LocalDateTime.now());
            flavor.setDishId(dish.getId());
            result = dishFlavorsMapper.add(flavor);
        }

        int add = dishMapper.add(dish);
        return add * result > 0;


    }

    @Override
    public List<DishByCategoryIdVo> selectAllByCategoryId(String categoryId, Integer status) {
        List<DishByCategoryIdVo> list = dishMapper.selectAllByCategoryId(categoryId, status);
        for (DishByCategoryIdVo dish : list) {
            List<GetDishFlavors> flavors = dishFlavorsMapper.selectById(dish.getId());
            GetCategoryVo getCategoryVo = categoryMapper.selectById(categoryId);
            String name = getCategoryVo.getName();
            Integer type = getCategoryVo.getType();
            dish.setType(type);
            dish.setCreateName(name);
            dish.setFlavors(flavors);
            list.add(dish);
        }


        return list;
    }


    @Override
    public boolean delete(String[] ids) {
        int result=0;
        int del1 ;
        int delete;
        for (String id : ids) {
            del1 = dishMapper.delete(id);
            delete = dishFlavorsMapper.delete(id);

           result += del1+ delete;

        }


        return result > 0;
    }

    @Override
    public boolean update(DishUpdateDto dish) {
        dish.setUpdateTime(LocalDateTime.now());
        String token = AuthThreadLocal.getAuth();
        String username = JwtToken.parse(token);
        dish.setUpdateUser(username);
        int update1 = dishMapper.update(dish);
        int update2=0;
        for (DishFlavors flavor : dish.getFlavors()) {
           flavor.setUpdateTime(LocalDateTime.now());
           flavor.setUpdateUser(username);
             update2 = dishFlavorsMapper.update(flavor);
             update2+=update2;
        }



        return update1*update2>0;


    }

    @Override
    public boolean status(Integer num, String[] ids) {
        int result = 0;
        for (String id : ids) {

            DishUpdateDto dish = new DishUpdateDto();
            dish.setId(id);
            dish.setStatus(num);
            result += dishMapper.update(dish);

        }
        return result > 0;
    }
}
